package com.example.jf.jfapp.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.activity.ImagePagerActivity;

import java.util.ArrayList;
import java.util.List;

import static com.example.jf.jfapp.activity.ImagePagerActivity.EXTRA_IMAGE_URLS;
import static com.example.jf.jfapp.activity.ImagePagerActivity.IMAGE_INDEX;

/**
 * Created by jf on 2017/7/29.
 */

public class StatuImageAdapter extends RecyclerView.Adapter<StatuImageAdapter.ItemViewHolder> {
    List<String> images;

    public StatuImageAdapter(List<String> images) {
        this.images = images;
    }

    @Override
    public StatuImageAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_statu_image, parent, false);
        StatuImageAdapter.ItemViewHolder itemViewHolder = new StatuImageAdapter.ItemViewHolder(view);
        return itemViewHolder;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }


    @Override
    public void onBindViewHolder(final StatuImageAdapter.ItemViewHolder holder, final int position) {
        Glide.with(holder.itemView.getContext())
                .load(images.get(position))
                .error(R.mipmap.ic_error)//失败的显示的图片
//                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE 缓存
                //三级缓存
                //1. 内存缓存,保存到运行内存中，ram
                //2. 保存到本地，sd卡中
                //3. 保存到网络中
                .into(holder.iv);


        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(holder.itemView.getContext(), ImagePagerActivity.class);
                intent.putExtra(IMAGE_INDEX, position);
                intent.putStringArrayListExtra(EXTRA_IMAGE_URLS, (ArrayList<String>) images);
                holder.iv.getContext().startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return images.size();
    }
}

